<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <form action="http://ajax-api.itheima.net/api/data" method="post">
      <input
        type="text"
        class="username"
        name="username"
        placeholder="请输入用户名"
      />
      <br />
      <input type="text" name="food" placeholder="请输入喜欢的食物" />
      <br />
      <input type="textarea" name="sign" placeholder="请输入个性签名" />
      <br />
      <button type="submit">提交</button>
    </form>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <script src="./lib/form-serialize.js"></script>
    <script>
      // 阻止默认行为
      //   document.querySelector('button').onclick = function (e) {
      //     e.preventDefault()
      //   }
      // 二选一阻止
      document.querySelector('form').onsubmit = function (e) {
        e.preventDefault()
        // 用axios发起请求
        //serialize作用： 一次性获取form表单中的内容
        const form = document.querySelector('form')
        const data1 = serialize(form, { hash: true })
        console.log(data1)

        axios({
          url: 'http://ajax-api.itheima.net/api/data',
          method: 'post',
          data: data1,
        }).then((res) => console.log(res))
      }
    </script>
  </body>
</html>
